package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.domain.Transfer;
import com.ruoyi.basedata.domain.UsersXb;
import com.ruoyi.basedata.service.ITransferService;
import com.ruoyi.basedata.service.IUsersXbService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 过户记录Controller
 * 
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/basedata/transfer")
public class TransferController extends BaseController
{
    @Autowired
    private ITransferService transferService;

    @Autowired
    private IUsersXbService usersXbService;

    /**
     * 查询过户记录列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Transfer transfer)
    {
        startPage();
        List<Transfer> list = transferService.selectTransferList(transfer);
        List<String> oldUserIdList = CollectionUtils.convertList(list, Transfer::getOldUserid);
        List<String> newUserIdList = CollectionUtils.convertList(list, Transfer::getNewUserid);
        Map<String, UsersXb> oldUserMap = usersXbService.getUserMap(oldUserIdList);
        Map<String, UsersXb> newUserMap = usersXbService.getUserMap(newUserIdList);
        list.forEach(transfer1 -> {
            if (oldUserMap.containsKey(transfer1.getOldUserid())){
                transfer1.setOldUserName(oldUserMap.get(transfer1.getOldUserid()).getUserName());
            }
            if (newUserMap.containsKey(transfer1.getNewUserid())){
                transfer1.setNewUserName(newUserMap.get(transfer1.getNewUserid()).getUserName());
            }

        });
        return getDataTable(list);
    }

    /**
     * 导出过户记录列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:transfer:export')")
    @Log(title = "过户记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Transfer transfer)
    {
        List<Transfer> list = transferService.selectTransferList(transfer);
        ExcelUtil<Transfer> util = new ExcelUtil<Transfer>(Transfer.class);
        util.exportExcel(response, list, "过户记录数据");
    }

    /**
     * 获取过户记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:transfer:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(transferService.selectTransferByID(ID));
    }

    /**
     * 新增过户记录
     */
    @PreAuthorize("@ss.hasPermi('basedata:transfer:add')")
    @Log(title = "过户记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Transfer transfer)
    {
        return toAjax(transferService.insertTransfer(transfer));
    }

    /**
     * 修改过户记录
     */
    @PreAuthorize("@ss.hasPermi('basedata:transfer:edit')")
    @Log(title = "过户记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Transfer transfer)
    {
        return toAjax(transferService.updateTransfer(transfer));
    }

    /**
     * 删除过户记录
     */
    @PreAuthorize("@ss.hasPermi('basedata:transfer:remove')")
    @Log(title = "过户记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(transferService.deleteTransferByIDs(IDs));
    }
}
